package com.hanbang.maige.pressenter.othre;

import android.view.Gravity;

import com.hanbang.maige.code.base.presenter.BasePresenter;
import com.hanbang.maige.mode.HttpRequest;
import com.hanbang.maige.mode.httpresponse.BaseHttpResponse;
import com.hanbang.maige.mode.httpresponse.HttpResult;
import com.hanbang.maige.mode.javabean.base.UserData;
import com.hanbang.maige.utils.http.httpquest.HttpCallBack;
import com.hanbang.maige.utils.http.httpquest.HttpRequestParam;
import com.hanbang.maige.utils.ui.ToastUtils;
import com.hanbang.maige.view.login.ILogInBaseView;

import org.json.JSONException;
import org.json.JSONObject;

import static com.hanbang.maige.code.MyApplication.myApp;

/**
 * Created by yang on 2016/8/17.
 */
public class LoginPresenter extends BasePresenter<ILogInBaseView.IloginView> {
    /**
     * 作者　　: 李坤
     * 创建时间: 2016/10/26 9:31
     * <p>
     * 方法功能：登录	Login	telphone：手机号
     * code:验证码
     */


    public void login(String telphone, String code) {

        HttpCallBack.Buider buider = new HttpCallBack
                .Buider(this)
                .setShowLoadding(true)
                .setHint("正在登陆");

        HttpCallBack httpCallBack = new HttpCallBack<HttpResult<UserData>>(buider) {

            @Override
            public void onSuccess(HttpResult<UserData> result) {
                super.onSuccess(result);

                //进行data处理
                if (result.isSucceed()) {
                    if (result.getData().setDbUserData()) {
                        mvpView.login(result.getData(), result.getMsg());
                    } else {
                        mvpView.showErrorSnackbar("登陆失败，用户信息错误");
                    }
                } else {
                    mvpView.showInfoSnackbar(result.msg);
                }

            }
        };
        HttpRequestParam param = new HttpRequestParam("Login");
        param.addParam("telphone", telphone);
        param.addParam("code", code);
        mvpView.addSubscription(HttpRequest.executePost(httpCallBack, param));
    }

    /**
     * 作者　　: 李坤
     * 创建时间: 2016/10/26 9:02
     * <p>
     * 方法功能：
     * 注册	Register
     * telphone：手机
     * code:验证码
     * BiaoQian:标签
     */

    public void postZhuChe(String telphone, String code, int biaoqian) {

        HttpCallBack.Buider buider = new HttpCallBack.Buider(this)
                .setShowLoadding(true)
                .setHint("正在注册");

        HttpCallBack httpCallBack = new HttpCallBack<HttpResult<UserData>>(buider) {

            @Override
            public void onSuccess(HttpResult<UserData> result) {
                super.onSuccess(result);
                //进行data处理
                if (result.isSucceed()) {
                    if (result.getData().setDbUserData()) {
                        mvpView.zhuche(result.getData(), result.getMsg());
                    } else {
                        ToastUtils.show(myApp, "注册失败，用户信息错误", ToastUtils.LENGTH_SHORT, Gravity.CENTER, 0, 0);
                    }
                } else {
                    ToastUtils.show(myApp, result.getMsg(), ToastUtils.LENGTH_SHORT, Gravity.CENTER, 0, 0);
                }
            }

            @Override
            public void onError(Throwable error) {
                super.onError(error);
                ToastUtils.show(myApp, "注册失败", ToastUtils.LENGTH_SHORT, Gravity.CENTER, 0, 0);
            }
        };
        HttpRequestParam param = new HttpRequestParam("Register");
        param.addParam("telphone", telphone);
        param.addParam("code", code);
        param.addParam("BiaoQian", biaoqian);
        mvpView.addSubscription(HttpRequest.executePost(httpCallBack, param));
    }

    /**
     * 作者　　: 李坤
     * 创建时间: 2016/10/26 9:02
     * <p>
     * 方法功能
     * 发送验证码，登录	LoginCode	telphone
     */

    public void getLogInCode(String telphone) {

        HttpCallBack.Buider buider = new HttpCallBack.Buider(this)
                .setShowLoadding(true)
                .setHint("发送验证码...");

        HttpCallBack httpCallBack = new HttpCallBack<BaseHttpResponse>(buider) {

            @Override
            public void onSuccess(BaseHttpResponse result) {
                super.onSuccess(result);

                mvpView.showInfoSnackbar(result.msg);
                //进行data处理
                if (result.isSucceed()) {
                    try {
                        mvpView.logInCode(new JSONObject(result.json).getString("code"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                }

            }
        };
        HttpRequestParam param = new HttpRequestParam("LoginCode");
        param.addParam("telphone", telphone);
        mvpView.addSubscription(HttpRequest.executePost(httpCallBack, param));
    }

    /**
     * 作者　　: 李坤
     * 创建时间: 2016/10/26 9:02
     * <p>
     * 方法功能：发送验证码，注册	RegisterCode	telphone
     */

    public void getZhuCheCode(String telphone) {

        HttpCallBack.Buider buider = new HttpCallBack.Buider(this)
                .setShowLoadding(true)
                .setHint("发送验证码...");

        HttpCallBack httpCallBack = new HttpCallBack<BaseHttpResponse>(buider) {

            @Override
            public void onSuccess(BaseHttpResponse result) {
                super.onSuccess(result);

                ToastUtils.show(myApp, result.getMsg(), ToastUtils.LENGTH_SHORT, Gravity.CENTER, 0, 0);
                //进行data处理
                if (result.isSucceed()) {
                    try {
                        mvpView.zhuCheCode(new JSONObject(result.json).getString("code"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                }

            }
        };
        HttpRequestParam param = new HttpRequestParam("RegisterCode");
        param.addParam("telphone", telphone);
        mvpView.addSubscription(HttpRequest.uploadFiles(httpCallBack, param));
    }


}
